package com.pingan.haofang.searchcloud.web.monitor.controller;

import com.pingan.haofang.searchcloud.common.constants.WebConstants;
import com.pingan.haofang.searchcloud.index.dto.IndexDTO;
import com.pingan.haofang.searchcloud.index.service.IndexService;
import com.pingan.haofang.searchcloud.monitor.service.DashboardIndexSummaryInfoService;
import com.pingan.haofang.searchcloud.user.dto.SearchProjectDto;
import com.pingan.haofang.searchcloud.user.service.SearchProjectService;
import com.pingan.haofang.searchcloud.web.monitor.form.DateRangeForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(WebConstants.WEB_PREFIX + "/dashboardCommon")
@Api(tags = "dashboard.common", description = "通用视图")
public class DashboardCommonController {

    @Autowired
    private SearchProjectService projectService;

    @Autowired
    private IndexService indexService;

    @Autowired
    private DashboardIndexSummaryInfoService indexSummaryInfoService;

    @ApiOperation("获取所有正常项目")
    @GetMapping("/projects")
    public List<SearchProjectDto> getProjects() {
        // 查询所有项目
        return projectService.findEnableProjects();
    }

    @ApiOperation("获取项目下所有正常索引")
    @GetMapping("/project/{projectId}/indexes")
    public List<IndexDTO> getIndexes(@PathVariable(value = "projectId", name = "projectId") long projectId) {
        // 查询项目下所有索引
        return indexService.queryByProjectId(projectId);
    }

    @ApiOperation("索引构建数据汇总")
    @RequestMapping(value = "/buildIndexSummaryInfo", method = RequestMethod.GET)
    public void buildIndexSummaryInfo(DateRangeForm form) throws Exception {
        Date[] rangeTime = getRangeTime(form.getFromDate());
        indexSummaryInfoService.buildIndexSummaryInfo(rangeTime[0], rangeTime[1]);
    }

    @ApiOperation("索引构建监控")
    @RequestMapping(value = "/monitorIndexBuild", method = RequestMethod.GET)
    public void monitorIndexBuild(DateRangeForm form) throws Exception {
        indexSummaryInfoService.monitorIndexBuild(form.getFromDate(), form.getToDate());
    }

    
    public Date[] getRangeTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DATE, -1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date fromTime = c.getTime();

        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        Date toTime = c.getTime();

        return new Date[] { fromTime, toTime };
    }
}
